package com.qch.demo;

import sun.misc.Launcher;

import java.net.URL;
import java.util.Properties;

/**
 * @Author qch
 * @Date 2021/8/9
 * 获取不同的类加载器
 * 启动类加载器通过代码的方式是无法获取到的
 *
 */
public class GetClassLoaderDemo2 {
    public static void main(String[] args) {
        //获取系统类加载器
        ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
        System.out.println(systemClassLoader);

        //获取扩展类加载器
        ClassLoader extClassloader = systemClassLoader.getParent();
        System.out.println(extClassloader);
        //获取启动类加载器--》结果是null
        ClassLoader bootstrapLoader = extClassloader.getParent();
        System.out.println(bootstrapLoader);
        //获取自定义类加载器->结果与扩展器类加载器一样r
        ClassLoader classLoader = GetClassLoaderDemo2.class.getClassLoader();
        System.out.println(classLoader);

        //通过启动类加载器的对象获取启动类加载器--》结果是null
        ClassLoader bootstrapLoader2 = String.class.getClassLoader();
        System.out.println(bootstrapLoader2);

        System.out.println("--------------启动引导类加载器------------------------");
        URL[] urLs = Launcher.getBootstrapClassPath().getURLs();
        for (URL urL : urLs) {
            System.out.println(urL);
        }
        System.out.println("--------------扩展类加载器------------------------");
        String properties = System.getProperty("java.ext.dirs");
        String[] split = properties.split(";");
        for (String s : split) {
            System.out.println();
        }
    }
}
